package com.chuangjiangx.member.coupon.ddd.domain.repository;

import com.chuangjiangx.dddbase.LongIdentity;
import com.chuangjiangx.dddbase.Repository;
import com.chuangjiangx.member.coupon.dao.mapper.InMbrCouponUseStoreMapper;
import com.chuangjiangx.member.coupon.dao.model.InMbrCouponUseStore;
import com.chuangjiangx.member.coupon.dao.model.InMbrCouponUseStoreExample;

import com.chuangjiangx.member.coupon.ddd.domain.model.MbrCouponId;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrCouponUseStoreId;
import com.chuangjiangx.member.basic.ddd.domain.model.StoreId;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrCouponUseStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Component
public class MbrCouponUseStoreRepository implements Repository<MbrCouponUseStore, MbrCouponUseStoreId> {

    private final InMbrCouponUseStoreMapper inMbrCouponUseStoreMapper;

    @Autowired
    public MbrCouponUseStoreRepository(InMbrCouponUseStoreMapper inMbrCouponUseStoreMapper) {
        this.inMbrCouponUseStoreMapper = inMbrCouponUseStoreMapper;
    }

    @Override
    public MbrCouponUseStore fromId(MbrCouponUseStoreId id) {
        return null;
    }

    @Override
    public void update(MbrCouponUseStore entity) {

    }

    @Override
    public void save(MbrCouponUseStore entity) {
        Objects.requireNonNull(entity);
        InMbrCouponUseStore in = convertToIn(entity);
        inMbrCouponUseStoreMapper.insertSelective(in);
        entity.setId(new MbrCouponUseStoreId(in.getId()));
    }

    /**
     * 删除卡券关联门店关系
     */
    public void deleteByMbrCouponId(MbrCouponId mbrCouponId) {
        List<InMbrCouponUseStore> in = fromMbrCouponId(mbrCouponId);
        for (InMbrCouponUseStore inMbrCouponUseStore : in) {
            inMbrCouponUseStoreMapper.deleteByPrimaryKey(inMbrCouponUseStore.getId());
        }
    }


    /**
     * 根据卡券id查询
     */
    public List<InMbrCouponUseStore> fromMbrCouponId(MbrCouponId mbrCouponId) {
        Objects.requireNonNull(mbrCouponId,"会员卡券id不能为空");
        InMbrCouponUseStoreExample example = new InMbrCouponUseStoreExample();
        example.createCriteria().andMbrCouponIdEqualTo(mbrCouponId.getId());
        return inMbrCouponUseStoreMapper.selectByExample(example);
    }


    private InMbrCouponUseStore convertToIn(MbrCouponUseStore mbrCouponUseStore) {
        Objects.requireNonNull(mbrCouponUseStore);
        InMbrCouponUseStore in = new InMbrCouponUseStore();

        in.setId(Optional.of(mbrCouponUseStore)
                .map(MbrCouponUseStore::getId)
                .map(LongIdentity::getId).orElse(null));

        in.setMbrCouponId(Optional.of(mbrCouponUseStore)
                .map(MbrCouponUseStore::getMbrCouponId)
                .map(LongIdentity::getId).orElse(null));

        in.setStoreId(Optional.of(mbrCouponUseStore)
                .map(MbrCouponUseStore::getStoreId)
                .map(LongIdentity::getId).orElse(null));
        return in;
    }

    private MbrCouponUseStore convertToEntity(InMbrCouponUseStore in) {
        Objects.requireNonNull(in);
        MbrCouponUseStore mbrCouponUseStore = new MbrCouponUseStore(
                in.getMbrCouponId() == null ? null : new MbrCouponId(in.getMbrCouponId()),
                in.getStoreId() == null ? null : new StoreId(in.getStoreId())
        );
        mbrCouponUseStore.setId(new MbrCouponUseStoreId(in.getId()));
        return mbrCouponUseStore;
    }
}
